#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/string_ext"

sum = { 'node-loads' => 0, 'node-load-misses' => 0, 'node-stores' => 0, 'node-store-misses' => 0 }
$stdin.each_line do |line|
  line = line.resolve_invalid_bytes

  _time, value, key = line.split
  next unless value =~ /^\d+$/

  sum[key] += value.to_i
end

node_local_load_ratio = if (sum['node-loads'] + sum['node-load-misses']).nonzero?
                          (100 * sum['node-loads']) / (sum['node-loads'] + sum['node-load-misses'])
                        else
                          -1
                        end

node_local_store_ratio = if (sum['node-stores'] + sum['node-store-misses']).nonzero?
                           (100 * sum['node-stores']) / (sum['node-stores'] + sum['node-store-misses'])
                         else
                           -1
                         end

puts "node-loads: #{sum['node-loads']}"
puts "node-load-misses: #{sum['node-load-misses']}"
puts "node-stores: #{sum['node-stores']}"
puts "node-store-misses: #{sum['node-store-misses']}"
puts "node-local-load-ratio: #{node_local_load_ratio}" if node_local_load_ratio != -1
puts "node-local-store-ratio: #{node_local_store_ratio}" if node_local_store_ratio != -1
